package com.kylin.module.study.business;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.kylin.common.Constants;
import com.kylin.common.dict.orm.Dict;
import com.kylin.common.exception.BusinessException;
import com.kylin.common.util.FileOperater;
import com.kylin.hibernate.BaseHibernateDAO;
import com.kylin.module.study.forms.CoursewareForm;
import com.kylin.module.study.orm.Courseware;

public class CoursewareBlogic {
	protected static Logger log = Logger.getLogger(CoursewareBlogic.class);

	/**
	 * 添加学习课件
	 * 
	 * @param form
	 */
	public void addCourseware(CoursewareForm form) throws BusinessException {
		log.debug("插入新的课件 ：" + form.getTitle());
		Courseware obj = new Courseware();
		obj.setActive(new Short("1"));
		//上传声音文件
		if(form.getMediapath()!=null && form.getMediapath().getFileSize() > 0){
			String audioPath = StudyBlogic.uploadMedia(Constants.MEDIA_COURSE, form.getMediapath(), null);
		    obj.setMediapath(audioPath);
		}
		if(form.getActive() != null && !form.getActive().equals(""))
		{
			List list = BaseHibernateDAO.findAll(Courseware.class,null);
			if(list.isEmpty())
			{
				for(int i=0;i<list.size();i++)
				{
					Courseware co = (Courseware)list.get(i);
					if(co.getActive().equals(Courseware.COURSEWARE_TOP))
					{
						co.setActive(new Short("1"));
						BaseHibernateDAO.update(co);
					}
				}
			}
		}
		obj.setArticleDict(new Dict(form.getCatarticleid()));
		obj.setDict(new Dict(form.getCatcoursewareid()));
		obj.setCreatedon(new Date());
		obj.setLanguage(form.getLanguage());
		obj.setTargetlang(form.getTargetlang());
		obj.setTitle(form.getTitle());
		obj.setContent(form.getContent());
		obj.setTranslatation(form.getTranslatation());
		obj.setActive(form.getActive());
		BaseHibernateDAO.save(obj);
	}
	
	/**
	 * 修改学习课件
	 * 
	 * @param form
	 */
	public void updateCourseware(CoursewareForm form) throws BusinessException {
		log.debug("修改课件 ：" + form.getTitle());
		Courseware obj = (Courseware) BaseHibernateDAO.findById(Courseware.class, form.getId());
		//上传 修改 声音文件
		if(form.getMediapath()!=null || form.getMediapath().getFileSize() > 0){
			String audioPath = StudyBlogic.uploadMedia(Constants.MEDIA_ARTICLE, form.getMediapath(), obj.getMediapath());
		    obj.setMediapath(audioPath);
		}
		obj.setArticleDict(new Dict(form.getCatarticleid()));
		obj.setDict(new Dict(form.getCatcoursewareid()));
		obj.setLastupdatedon(new Date());//最后修改时间
		obj.setTitle(form.getTitle());
		obj.setContent(form.getContent());
		obj.setTranslatation(form.getTranslatation());
		BaseHibernateDAO.update(obj);
	}

	/**
	 * 根据ID获得课件
	 * 
	 * @param id
	 */
	public Courseware getCoursewareById(Long id) throws BusinessException {
		return (Courseware) BaseHibernateDAO.findById(Courseware.class, id);
	}

	/**
	 * 根据ID删除课件 
	 * 
	 * @param id
	 */
	public void deleteById(Long id) throws BusinessException {
		Courseware obj = getCoursewareById(id);
		if (obj != null) {
			BaseHibernateDAO.delete(obj);
			try {
				FileOperater.deleteFile(Constants.MEDIA_COURSE, obj.getMediapath());
			} catch (IOException e) {
				e.printStackTrace();
				throw new BusinessException("删除课件附属声音文件失败："+Constants.MEDIA_COURSE +File.separator+ obj.getMediapath());
			}
		}
	}
}
